package com.atguigu.tingshu.order.pattern.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.order.pattern.AbstractTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Component(SystemConstant.ORDER_ITEM_TYPE_ALBUM)
public class AlbumTrade extends AbstractTradeStrategy {

    /**
     * 对专辑进行结算
     * @param tradeVo
     * @param userId
     * @return
     */
    @Override
    public OrderInfoVo trade(TradeVo tradeVo, Long userId) {

        log.info("对专辑进行结算");


        String itemType = tradeVo.getItemType();

        Long itemId = tradeVo.getItemId();

        Integer trackCount = tradeVo.getTrackCount();

        //先初始化原始数据
        OrderInfoVo orderInfoVo = new OrderInfoVo();

        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();

        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();

        //初始化原始订单金额
        BigDecimal originalAmount = new BigDecimal("0.00");

        //初始化折扣订单金额
        BigDecimal derateAmount = new BigDecimal("0.00");

        //初始化订单订单金额
        BigDecimal orderAmount = new BigDecimal("0.00");

        //封装时间戳
        orderInfoVo.setTimestamp(System.currentTimeMillis());
        //签名机制
        //调用生成签名的工具方法,底层就是MD5,对订单VO中所有属性值进行排序,拼接成字符串加密钥,进行MD5加密,返回签名值
        //将订单VO转为map对象,由于无法确定用户选择用什么支付方式,所以需要排出payway

        Map<String, Object> map = BeanUtil.beanToMap(orderInfoVo, false, true);
        //调用生成签名工具方法，底层就是Md5,对订单VO中所有属性值进行排序，拼接成字符串+秘钥，进行MD5加密，返回签名值
        String sign = SignHelper.getSign(map);

        orderInfoVo.setSign(sign);

        //5.封装订单VO对象
        //5.1 封装相关价格信息
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        //5.2 封装商品相关集合
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        //封装类型
        orderInfoVo.setItemType(itemType);


        return orderInfoVo;
    }
}
